<?php

namespace addons\cms\model;

use app\common\library\Auth;
use app\common\library\Email;
use think\Exception;
use think\Model;
use think\Validate;
use app\common\model\User;


/**
 * 标签模型
 */
class Sign Extends Model
{

    protected $name = "cms_sign";

    /**
     * 发表评论
     * @param array $params
     * @return bool
     * @throws Exception
     */
    public static function sign($sign_date)
    {
        $request = request();
        $request->filter('strip_tags');
        $useragent = $request->server('HTTP_USER_AGENT', '');
        $ip = $request->ip();
        $auth = Auth::instance();

        if (!$auth->id) {
            return [
                'code'=>-1,
                'msg'=>"请登录后进行签到"
            ];
        }
        //查找是否关注
        $is_sign = self::where(['sign_date' =>$sign_date, 'user_id' =>  $auth->id])->find();
        if ($is_sign) {
            return [
                'code'=>-1,
                'msg'=>"你今天已签到了"
            ];
        }

        $params['user_id'] = $auth->id;
        $params['sign_date'] = $sign_date;
        (new static())->allowField(true)->save($params);

        //连续签到多少天
        $root['code'] = 1;
        $root['even_sign'] = (new static())->get_even_sign(collection(self::where(['user_id' =>  $auth->id])->order('sign_date','desc')->select())->toArray(),0);
        $root['count_sign']  = self::where(['user_id' =>  $auth->id])->count();
        $root['is_sign'] = 1;

        if($root['even_sign']%7 == 0){
            $root['code'] = 2;
            //增加作者余额
            User::where('id', $auth->id)->setInc('money', 1);
        }
        return $root;
    }

    /**
     * 发表评论
     * @param array $params
     * @return bool
     * @throws Exception
     */
    public static function get_sign()
    {
        $request = request();
        $request->filter('strip_tags');
        $useragent = $request->server('HTTP_USER_AGENT', '');
        $ip = $request->ip();
        $auth = Auth::instance();

        if (!$auth->id) {
            return [
                'code'=>-1,
                'msg'=>"请登录后进行签到"
            ];
        }
        //查找是否关注
        $is_sign = self::where(['sign_date' =>date("Y-m-d",time()), 'user_id' =>  $auth->id])->find();

        //连续签到多少天
        $root['code'] = 1;
        $root['is_sign'] = $is_sign?1:0;
        $root['even_sign'] = (new static())->get_even_sign(collection(self::where(['user_id' =>  $auth->id])->order('sign_date','desc')->select())->toArray(),$is_sign?0:1);
        $root['count_sign']  = self::where(['user_id' =>  $auth->id])->count();
        return $root;
    }

    public function get_even_sign($sign_data,$is_sign){
        //时间
        if($is_sign){
            $date = date("Y-m-d",time()-60*60*24);
        }else{
            $date = date("Y-m-d",time());
        }
        //连续签到次数
        $maxnum = 0;

        foreach ($sign_data as $k=>$v){
            if($date == $v['sign_date']){
                $maxnum++;
                $date = date("Y-m-d",strtotime($date)-60*60*24);
            }else{
                break;
            }
        }
        return $maxnum;
    }

}
